#include "bresponse.h"
#include "butils.h"

using namespace bhttp;

void bresponse::successed(const bool succ)
{
	successed_ = succ;
}

bool bresponse::successed() const
{
	return successed_;
}

void bresponse::body(const std::vector<char>& data)
{
    body_ = data;
}

void bresponse::body(std::vector<char>&& data)
{
    body_ = std::move(data);
}

std::vector<char>& bresponse::body()
{
	return body_;
}

std::string bresponse::body_as_string() const
{
	if (body_.empty()) return std::string();
	return std::string(body_.data(), body_.size());
}

void bresponse::headers(const std::vector<char>& rh)
{
	headers_ = rh;
}

void bresponse::headers(std::vector<char>&& rh)
{
	headers_ = std::move(rh);
}

std::vector<char>& bresponse::headers()
{
	return headers_;
}

std::string bresponse::header(const std::string& key)
{
    std::vector<std::string> hds = butils::split(std::string(headers_.data(), headers_.size()), "\r\n");
    std::string k, v;
    for (const auto& hd : hds)
    {
        if (hd.find(":") == std::string::npos)
            continue;
        k.clear();
        v.clear();
        butils::split(hd, ":", &k, &v);
        if (butils::strip(key) == k)
            return v;
    }
    return std::string();
}

void bresponse::code(int code)
{
	code_ = code;
}

int bresponse::code() const
{
	return code_;
}

char* bresponse::error()
{
	return error_buffer_;
}
